// ***** This file is automatically generated from Reverse.java.jpp

package daikon.inv.binary.twoSequence;

import daikon.*;
import daikon.inv.*;
import java.util.logging.Logger;
import java.util.logging.Level;

/**
 * Represents two sequences of double where one is in the reverse order
 * of the other.  Prints as <samp>x[] is the reverse of y[]</samp>.
 **/
public class ReverseFloat
  extends TwoSequenceFloat
{
  // We are Serializable, so we specify a version to allow changes to
  // method signatures without breaking serialization.  If you add or
  // remove fields, you should change this number to the current date.
  static final long serialVersionUID = 20030822L;

  public static final Logger debug =
    Logger.getLogger("daikon.inv.binary.twoSequence.ReverseFloat");

  // Variables starting with dkconfig_ should only be set via the
  // daikon.config.Configuration interface.
  /**
   * Boolean.  True iff Reverse invariants should be considered.
   **/
  public static boolean dkconfig_enabled = true;

  protected ReverseFloat(PptSlice ppt) {
    super(ppt);
  }

  protected /*@Prototype*/ ReverseFloat() {
    super();
  }

  private static /*@Prototype*/ ReverseFloat proto;

  /** Returns the prototype invariant for ReverseFloat **/
  public static /*@Prototype*/ ReverseFloat get_proto() {
    if (proto == null)
      proto = new /*@Prototype*/ ReverseFloat ();
    return proto;
  }

  /** returns whether or not this invariant is enabled **/
  public boolean enabled() {
    return dkconfig_enabled;
  }

  /** Reverse only makes sense on ordered arrays **/
  public boolean instantiate_ok (VarInfo[] vis) {

    if (!valid_types (vis))
      return (false);

    // Check to see that both arrays are ordered
    if (!vis[0].aux.getFlag(VarInfoAux.HAS_ORDER) ||
        !vis[1].aux.getFlag(VarInfoAux.HAS_ORDER))
      return (false);

    return (true);
  }

  /** instantiates the invariant on the specified slice **/
  public ReverseFloat instantiate_dyn (PptSlice slice) /*@Prototype*/ {
    return new ReverseFloat(slice);
  }

  protected Invariant resurrect_done_swapped() {
    // "reverse of" is symmetric
    return this;
  }

  public String repr() {
    return "ReverseFloat" + varNames() + ": "
      + "falsified=" + falsified;
  }

  public String format_using(OutputFormat format) {
    if (format.isJavaFamily()) return format_java_family(format);

    if (format == OutputFormat.DAIKON) return format_daikon();
    if (format == OutputFormat.SIMPLIFY) return format_simplify();

    return format_unimplemented(format);
  }

  public String format_daikon() {
    return var1().name() + " is the reverse of " + var2().name();
  }

  public String format_java_family(OutputFormat format) {
          return "daikon.Quant.isReverse(" + var1().name_using(format)
            + ", " + var2().name_using(format) + ")";
  }

  public String format_simplify() {
    if (Invariant.dkconfig_simplify_define_predicates)
      return format_simplify_defined();
    else
      return format_simplify_explicit();
  }

  private String format_simplify_defined() {
    VarInfo onevar = var1();
    VarInfo othervar = var2();
    String[] one_name = onevar.simplifyNameAndBounds();
    String[] other_name = othervar.simplifyNameAndBounds();

    if (one_name == null || other_name == null) {
      return "format_simplify() can't handle one of these sequences: "
        + format();
    }

    return "(|is-reverse-of| " +
      one_name[0] + " " + one_name[1] + " " + one_name[2] + " " +
      other_name[0] + " " + other_name[1] + " " + other_name[2] + ")";
  }

  private String format_simplify_explicit() {
    VarInfo onevar = var1();
    VarInfo othervar = var2();
    String[] one_name = onevar.simplifyNameAndBounds();
    String[] other_name = othervar.simplifyNameAndBounds();

    if (one_name == null || other_name == null) {
      return "format_simplify() can't handle one of these sequences: "
        + format();
    }

    String index = VarInfo.get_simplify_free_index (onevar, othervar);

    // (FORALL (a i j b ip jp)
    //  (IFF (|is-reverse-of| a i j b ip jp)
    //   (AND (EQ (- j i) (- jp ip))
    //        (<= 0 i)
    //        (< j (arrayLength a))
    //        (<= 0 ip)
    //        (< jp (arrayLength b))
    //     (FORALL (x)
    //        (IMPLIES
    //            (AND (<= 0 x) (< x (- j i)))
    //            (EQ (select (select elems a) (+ i x))
    //                (select (select elems b) (- jp x))))))))

    return "(AND (EQ (- " + one_name[2] + " " + one_name[1] + ") (- "
      + other_name[2] + " " + other_name[1] + ")) (<= 0 " + one_name[1]
      + ") (< " + one_name[2] + " (arrayLength " + one_name[0]
      + ")) (<= 0 " + other_name[1] + ") (< " + other_name[2]
      + " (arrayLength " + other_name[0] + ")) (FORALL (" + index
      + ") (IMPLIES (AND (<= 0 " + index + ") (< " + index + " (- "
      + one_name[2] + " " + one_name[1] + "))) (EQ (select (select elems "
      + one_name[0] + ") (+ " + one_name[1] + " " + index
      + ")) (select (select elems " + other_name[0] + ") (- " + other_name[2]
      + " " + index + "))))))";
  }

  public InvariantStatus check_modified(double[] a1, double[] a2, int count) {
    if (a1.length != a2.length) {
      return InvariantStatus.FALSIFIED;
    }
    int len = a1.length;
    for (int i=0, j=len-1; i<len; i++, j--) {
      if (!Global.fuzzy.eq(a1[i], a2[j])) {
        return InvariantStatus.FALSIFIED;
      }
    }
    return InvariantStatus.NO_CHANGE;
  }

  public InvariantStatus add_modified(double[] a1, double[] a2, int count) {
    return check_modified(a1, a2, count);
  }

  protected double computeConfidence() {
    return Invariant.CONFIDENCE_JUSTIFIED;
  }

  public boolean isSameFormula(Invariant other) {
    assert other instanceof ReverseFloat;
    return true;
  }

}
